package br.com.amethyst.workflow.template;

import java.io.File;
import java.net.URI;
import javax.xml.transform.stream.StreamSource;
import br.com.amethyst.workflow.exception.WorkflowSourceLoadException;

/**
 * Basic Template class
 * @author Artur Figueira <arturfigueira@gmail.com>
 *
 */
public class Template implements ITemplate
{
	private StreamSource  source;
	
	/**
	 * Default constructor.
	 * Load a StreamSource from a given template file path
	 * @param templatePath
	 * @throws Exception
	 */
	public Template(String templatePath) throws WorkflowSourceLoadException
	{
		try{
			if(templatePath == null || "".equals(templatePath))
				throw new WorkflowSourceLoadException("Template can't be loaded because the path can't be null or empty");
			
			//Generate an Unique System location based on the element type
			URI systemURI = new File(templatePath).toURI();	
			
			//Generate A StreamSource from an Unique Location of the file depending on the OS		
			this.source = new StreamSource(systemURI.toURL().toExternalForm());
		}
		catch (Exception e) {
			throw new WorkflowSourceLoadException(e.getMessage());
		}
	}

	@Override
	public StreamSource getSource() 
	{
		return source;
	}

}
